import Vue from 'vue'

//格式化代码,去除行前空格
Vue.prototype.fCode = function (code) {
  let code_list = []
  code
    .trim()
    .split('\n')
    .forEach(function (v) {
      code_list.push(v)
    })

  let fcode = ''
  if (code_list.length > 1) {
    let length = code_list[code_list.length - 1].match(/(^(?:\s|\t)+)/)
    length = length[0].length
    for (let i = 1; i < code_list.length; i++) {
      code_list[i] = code_list[i].slice(length)
    }
    for (let i = 0; i < code_list.length; i++) {
      fcode = fcode + code_list[i] + '\n'
    }
  } else {
    fcode = code_list[0]
  }
  return fcode
}

//深克隆
Vue.prototype.deepClone = function deep(source) {
  if (typeof source == 'string' || source === null) {
    return source
  }
  // 校验当前类型是数组还是对象
  const target = Array.isArray(source) ? [] : {}
  for (const k in source) {
    const val = source[k]
    const valueType = typeof val
    if (valueType === 'function') {
      target[k] = new Function(`return ${val.toString()}`)()
    } else if (valueType === 'object') {
      // eslint-disable-next-line no-undef
      target[k] = deep(val)
    } else {
      target[k] = val
    }
  }
  return target
}
